package ar.com.globallogic.promomr.api.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.multipart.MultipartFile;

import ar.com.globallogic.promomr.domain.Content;
import ar.com.globallogic.promomr.domain.Publication;
import ar.com.globallogic.promomr.domain.User;
import ar.com.globallogic.promomr.services.ContentService;
import ar.com.globallogic.promomr.services.FileService;
import ar.com.globallogic.promomr.services.PublicationService;
import ar.com.globallogic.promomr.services.SecurityService;

@Controller
@RequestMapping("/api/library/contents")
public class ContentController {
		
	private ContentService contentService;
	private FileService fileService;
	private PublicationService publicationService;
	private SecurityService securityService;

	@Autowired
	public ContentController(ContentService contentService, FileService fileService, PublicationService publicationService, SecurityService securityService) {
		super();
		this.contentService = contentService;
		this.fileService = fileService;
		this.publicationService = publicationService;
		this.securityService = securityService;
	}
	
	@RequestMapping(method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.CREATED)
	public synchronized @ResponseBody Content addContent(@RequestBody Content content) {
		return contentService.save(content);
	}

	@RequestMapping(value="{id}",method = RequestMethod.GET)
	@ResponseStatus(value = HttpStatus.OK)
	public @ResponseBody Content getContent(@PathVariable String id) {
		Publication publication = publicationService.findByContentId(id);
		User authenticatedUser = securityService.getAuthenticatedUser();
		return publication.getContentForUser(authenticatedUser);
	}
	
	@RequestMapping(value="resources/{id}", method = RequestMethod.POST)
	@ResponseStatus(value = HttpStatus.CREATED)
	public @ResponseBody Content addContentResource(@PathVariable String id, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
		Content content = contentService.findOne(id);
		String fileName = fileService.getFileNameForContent(file, content); 
		fileService.save(fileName, file);
		content.setMimeType(file.getContentType());
		content.setUrl(getUrlForContent(request, fileName));
		contentService.update(content);
		User owner = securityService.getAuthenticatedUser();		
		Publication publication = new Publication(owner, content);
		publicationService.save(publication);
		return content;
	}
	
	private String getUrlForContent(HttpServletRequest request, String fileName) {
		return request.getRequestURL().toString().split(request.getServletPath())[0] + "/static/" + fileName;
	}
}
